package com.example.myanimator.one;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;


import androidx.annotation.Nullable;

import com.example.myanimator.R;
import com.example.myanimator.view.ILike;

import java.util.Arrays;

/**
 * @Author: LvWeiHao
 * @CreateDate: 2020/7/6 10:04
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/7/6 10:04
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 * @Description: 自定义滚动textView
 */
public class SlideTextView extends View implements ILike {
    private int mTextSize;
    private int mTextColor;
    private Paint mPaint;

    private int mWidth;
    private int mHeight;

    private ObjectAnimator animator;
    private float animProgress = 1;

    private int sumLike;
    private int oldSumLike;
    private int[] oldSumLikeArray = new int[11];
    private int[] nowSumLikeArray = new int[11];
    private int oldSumLikeArrayLength;
    private int nowSumLikeArrayLength;
    private boolean liked = false;
    private int mANumWidth;

    public SlideTextView(Context context) {
        this(context, null);
    }

    public SlideTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SlideTextView);
        mTextSize = typedArray.getDimensionPixelOffset(R.styleable.SlideTextView_textSize, 18);
        mTextColor = typedArray.getColor(R.styleable.SlideTextView_textColor, Color.BLACK);

        initView();
        initAnimate();

    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
    }

    private void initAnimate() {
        animator = ObjectAnimator.ofFloat(this, "animProgress", 0f, 0.5f);
//        animator.setDuration(5000);
    }

    private void analysisLikeNum() {
        oldSumLikeArrayLength = nowSumLikeArrayLength;
        //Arrays.copyOf()方法返回的数组是新的数组对象，原数组对象仍是原数组对象，不变，
        // 该拷贝不会影响原来的数组。copyOf()的第二个自变量指定要建立的新数组长度，
        // 如果新数组的长度超过原数组的长度，则保留数组默认值.
        oldSumLikeArray = Arrays.copyOf(nowSumLikeArray, oldSumLikeArrayLength);
        nowSumLikeArrayLength = String.valueOf(sumLike).length();
        for (int i = 0; i < nowSumLikeArrayLength; i++) {
            nowSumLikeArray[i] = (int) (sumLike / Math.pow(10, i) % 10);
        }
    }

    public void setIsLike(boolean isLike) {
        this.liked = isLike;
    }

    public void like() {
//        liked = true;
        oldSumLike = sumLike;
        sumLike++;
    }

    public void unLike() {
        liked = false;
        oldSumLike = sumLike;
        sumLike--;
    }

    @Override
    public boolean changeLike() {
        if (liked) {
            unLike();
        } else {
            like();
        }
        analysisLikeNum();
        changeWidth(sumLike);
        if (animator != null) {
            animator.start();
        }
        return liked;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mWidth = getWidth() - getPaddingLeft() - getPaddingRight();
        mHeight = getHeight() - getPaddingTop() - getPaddingBottom();
        mANumWidth = (int) mPaint.measureText("0");
        if (nowSumLikeArrayLength != oldSumLikeArray.length) {
            if (animProgress < 1) {
                mPaint.setAlpha((int) (255 - 255 * animProgress));
                for (int i = oldSumLikeArrayLength - 1; i >= 0; i--) {
                    canvas.drawText("" + oldSumLikeArray[i]
                            , (i != oldSumLikeArrayLength - 1 ? mANumWidth : 0) * (oldSumLikeArrayLength - 1 - i)
                            , mHeight / 2 - mPaint.descent() / 2 - mPaint.ascent() / 2 - (liked ? 1 : -1) * mTextSize * animProgress, mPaint);
                }
            }

            mPaint.setAlpha((int) (255 * animProgress));
            for (int i = nowSumLikeArrayLength - 1; i >= 0; i--) {
                canvas.drawText("" + nowSumLikeArray[i]
                        , (i != nowSumLikeArrayLength - 1 ? mANumWidth : 0) * (nowSumLikeArrayLength - 1 - i)
                        , mHeight / 2 - mPaint.descent() / 2 - mPaint.ascent() / 2 + (liked ? 1 : -1) * mTextSize * (1 - animProgress), mPaint);
            }
        } else {
            for (int i = oldSumLikeArrayLength - 1; i >= 0; i--) {
                // 画不变的部分
                if (oldSumLikeArray[i] == nowSumLikeArray[i]) {
                    mPaint.setAlpha(255);
                    canvas.drawText("" + nowSumLikeArray[i], (i != nowSumLikeArrayLength - 1 ? mANumWidth : 0) * (nowSumLikeArrayLength - 1 - i), mHeight / 2 - mPaint.descent() / 2 - mPaint.ascent() / 2, mPaint);
                    continue;
                }// 画变化的部分
                if (animProgress < 1) {
                    mPaint.setAlpha((int) (255 - 255 * animProgress));
                    canvas.drawText("" + oldSumLikeArray[i], (i != oldSumLikeArrayLength - 1 ? mANumWidth : 0) * (oldSumLikeArrayLength - 1 - i), mHeight / 2 - mPaint.descent() / 2 - mPaint.ascent() / 2 - (liked ? 1 : -1) * mTextSize * animProgress, mPaint);
                }

                mPaint.setAlpha((int) (255 * animProgress));
                canvas.drawText("" + nowSumLikeArray[i]
                        , (i != nowSumLikeArrayLength - 1 ? mANumWidth : 0) * (nowSumLikeArrayLength - 1 - i)
                        , mHeight / 2 - mPaint.descent() / 2 - mPaint.ascent() / 2 + (liked ? 1 : -1) * mTextSize * (1 - animProgress), mPaint);
            }
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int length = String.valueOf(sumLike).length();
        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension((int) mPaint.measureText("0") * length, (int) (mPaint.getFontMetrics().bottom - mPaint.getFontMetrics().top));
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension((int) (mPaint.measureText("0") * length), heightSize);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSize, (int) (mPaint.getFontMetrics().bottom - mPaint.getFontMetrics().top));
        }
    }

    public void setAnimProgress(float animProgress) {
        this.animProgress = animProgress;
        invalidate();
    }

    public int getSumLike() {
        return sumLike;
    }

    public void setSumLike(int sumLike) {
        this.sumLike = sumLike;
        analysisLikeNum();
        changeWidth(sumLike);
        invalidate();
    }

    private void changeWidth(int sumLike) {
        int length = String.valueOf(sumLike).length();
        int oldLength = String.valueOf(oldSumLike).length();
        if (length != oldLength) {
            requestLayout();
        }
        this.oldSumLike = sumLike;
    }

}
